package com.kun.demo01;

import org.junit.Test;

import java.sql.*;

public class JdbcStudy {


    @Test
    public void test() throws ClassNotFoundException, SQLException {
        //1. 加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");//固定写法
        //2. 用户信息和url
        //useUnicode=true&characterEncoding=utf8&&useSSL=true
        String url ="jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&&useSSL=false";
        String name = "root";
        String password = "123456";

        //3. 连接成功，返回数据库对象  connection代表数据库
        Connection connection= DriverManager.getConnection(url,name,password);
        //4. 执行SQL的对象 statement 执行SQL的对象
        Statement statement = connection.createStatement();

        //5. 执行SQL的对象 去执行SQL   可能存在结果，查看返回结果
        String sql="SELECT * FROM users";
        ResultSet resultSet = statement.executeQuery(sql);//返回的结果集,结果集中封装了我们全部查询的结果
        while(resultSet.next()){
            System.out.print("id: "+resultSet.getObject("id")+"\t");
            System.out.print("name: "+resultSet.getObject("NAME")+"\t");
            System.out.print("password: "+resultSet.getObject("PASSWORD")+"\t");
            System.out.print("email: "+resultSet.getObject("email")+"\t");
            System.out.print("birthday: "+resultSet.getObject("birthday")+"\n");
        }
        //6. 释放连接
        resultSet.close();
        statement.close();
        connection.close();
    }


}
